import datetime
from pydantic import BaseModel, Field, HttpUrl
from typing import List, Optional
import qiniu

# 七牛云配置
QINIU_ACCESS_KEY = 'JAo7YQ87r9La8fzS1SToxDdOaMywkKtn5CLDkzsl'
QINIU_SECRET_KEY = 'Wh5nGoBjH9aoIq2T72uymGeGjWrREzMB2YMxVPSG'
QINIU_BUCKET_DOMAIN = 'sz4rr0buo.hn-bkt.clouddn.com'

# 初始化七牛云鉴权对象
q = qiniu.Auth(QINIU_ACCESS_KEY, QINIU_SECRET_KEY)
# 商品分类
class ProductCategoryDSchema(BaseModel):
    id: int
    name: str
    description: Optional[str] = None
    image_url: str
    video_url: str
    price: float
    inventory: Optional[int] = 0  # 允许为None，默认值为0
    sales: Optional[int] = 0
    @property
    def full_image_url(self):
        return f'http://sxd275n7b.hd-bkt.clouddn.com/{self.image_url}'
    
    class Config:
        from_attributes = True

# 分类
class CategorySchema(BaseModel):
    id: int
    name: str
    description: Optional[str] = None
    parent_id: Optional[int] = None
    children: List['CategorySchema'] = []  # 用于嵌套子分类
    
    class Config:
        from_attributes = True  # Pydantic v2 的配置

# 修复循环引用
CategorySchema.model_rebuild()

# 商品
class ProductSchema(BaseModel):
    name: str
    price: float
    description: str | None = None  # 允许 description 为 None
    category: CategorySchema  
    image_url: str | None = None
    video_url: str = None

    @property
    def full_image_url(self):
        return f'http://sxd275n7b.hd-bkt.clouddn.com/{self.image_url}'

    class Config:
        from_attributes = True

# 规格
class ProductSpecSchemsa(BaseModel):
    name: str
    product: ProductSchema
    class Config:
        from_attributes = True
# 规格选项
class ProductSpecOptionSchemsa(BaseModel):
    name: str
    spec: ProductSpecSchemsa

    class Config:
        from_attributes = True

# 小料
class ProductToppingSchema(BaseModel):
    id: int
    name: str
    price: float  # 小料价格
    is_enabled: bool

    class Config:
        from_attributes = True

# 评论
class ProductCommentRequest(BaseModel):
    user_id: int
    order_id: int
    rating: int
    content: str
    created_at: datetime.datetime
    product_id: int
    images: Optional[str] = None
    is_auto: bool = False
    reply_content: Optional[str] = None  # 新增：回复内容

    class Config:
        from_attributes = True  # 确保与ORM模型兼容

class ProductCommentResponse(BaseModel):
    id: int
    product_id: int
    user_id: int
    order_id: int
    rating: int
    content: str
    created_at: datetime.datetime
    content: str
    images: Optional[str] = None
    is_auto: bool = False
    spec_option_id: Optional[int] = None  # 新增：购买的规格选项ID

    class Config:
        from_attributes = True

# 请求模型，用于接收回复评论的信息
class ProductCommentReplyRequest(BaseModel):
    content: str
    reply_content: str

# 响应模型，用于返回回复操作的结果
class ProductCommentReplyResponse(BaseModel):
    message: str